package org.zuel.lkq.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zuel.lkq.domain.Question;
import org.zuel.lkq.domain.QuestionExample;
import org.zuel.lkq.domain.Talk;
import org.zuel.lkq.enums.QStatusEnum;
import org.zuel.lkq.enums.RoleEnum;
import org.zuel.lkq.mapper.QuestionMapper;
import org.zuel.lkq.mapper.TalkMapper;
import org.zuel.lkq.mapper.UserMapper;
import org.zuel.lkq.param.user.talk.PickQuestionParam;
import org.zuel.lkq.param.user.talk.QueryQuestionParam;
import org.zuel.lkq.param.user.talk.TalkCreateParam;
import org.zuel.lkq.service.AdminTalkService;
import org.zuel.lkq.util.MySecurityUtil;
import org.zuel.lkq.util.ResponseUtil;
import org.zuel.lkq.viewmodel.admin.talk.MyQuestionInfoVM;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminTalkServiceImpl implements AdminTalkService {

    public QuestionMapper questionMapper;

    public UserMapper userMapper;

    public TalkMapper talkMapper;

    @Autowired
    public AdminTalkServiceImpl(QuestionMapper questionMapper,
                                UserMapper userMapper,
                                TalkMapper talkMapper) {
        this.questionMapper = questionMapper;
        this.userMapper = userMapper;
        this.talkMapper = talkMapper;
    }

    @Override
    public Object queryMyQuestion(QueryQuestionParam param) {
        if (param == null) {
            return ResponseUtil.badArgument();
        }

        QuestionExample example = new QuestionExample();
        example.setOrderByClause("add_time desc");
        QuestionExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false).andAnswerIdEqualTo(MySecurityUtil.getCurrentUserId());
        if (param.getTitle() != null) {
            criteria.andTitleLike("%" + param.getTitle() + "%");
        }
        if (param.getStatus() != null) {
            criteria.andQStatusEqualTo(param.getStatus());
        }
        List<MyQuestionInfoVM> vmList = getVMList(example);
        return ResponseUtil.ok(vmList);
    }

    @Override
    public Object queryVacantQuestion(QueryQuestionParam param) {
        if (param == null) {
            return ResponseUtil.badArgument();
        }
        QuestionExample example = new QuestionExample();
        example.setOrderByClause("add_time desc");
        QuestionExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false).andQStatusEqualTo(QStatusEnum.UNASSIGNED.getStatus());
        if (param.getTitle() != null) {
            criteria.andTitleLike("%" + param.getTitle() + "%");
        }
        List<MyQuestionInfoVM> vmList = getVMList(example);
        return ResponseUtil.ok(vmList);

    }

    private List<MyQuestionInfoVM> getVMList(QuestionExample example) {
        List<Question> questions = questionMapper.selectByExample(example);
        List<MyQuestionInfoVM> vmList = questions.stream().map(q -> {
            MyQuestionInfoVM vm = new MyQuestionInfoVM();
            BeanUtils.copyProperties(q, vm);
            vm.setPdPerson(userMapper.selectByPrimaryKey(q.getPdId()).getUsername());
            return vm;
        }).collect(Collectors.toList());
        return vmList;
    }

    @Override
    public Object pickQuestion(PickQuestionParam param) {
        if (param == null) {
            return ResponseUtil.badArgument();
        }
        Question q = new Question();
        q.setId(param.getId());
        q.setAnswerId(MySecurityUtil.getCurrentUserId());
        q.setqStatus(QStatusEnum.TALKING.getStatus());
        questionMapper.updateByPrimaryKeySelective(q);
        return ResponseUtil.ok("选单成功");
    }

    @Override
    public Object createTalkDetail(TalkCreateParam param) {
        if (param == null) {
            return ResponseUtil.badArgument();
        }
        Talk talk = new Talk();
        BeanUtils.copyProperties(param, talk);
        talk.setUserId(MySecurityUtil.getCurrentUserId());
        talk.setTalkType(RoleEnum.ADMIN.getType());
        talk.setAddTime(LocalDateTime.now());
        talkMapper.insertSelective(talk);
        return ResponseUtil.ok(talk);
    }

}
